﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using AttributeRouting;
using AttributeRouting.Web.Mvc;
using PH.Model.Schools;
using PH.Logic.Schools;
namespace PH.Web.Management.Areas.School.Controllers
{
    [RouteArea("School")]
    [RoutePrefix("Class")]
    [Authorize]
    public class ClassController : Controller
    {
        [Route("")]
        [Filters.CurrentSchool]
        public ActionResult Index()
        {
            return View();
        }

        [Route("Edit/{guid:guid?}")]
        [Filters.CurrentSchool]
        public ActionResult Edit(Guid? guid)
        {
            //ViewBag.MySchool = schGuid;
            ClassModel model = null;
            if (guid != null)
                model = ClassLogic.GetClass(guid.Value);
            else
                model = new ClassModel();
            return PartialView(model);
        }

        [Route("ExportTemplate")]
        [Filters.CurrentSchool]
        public ActionResult ExportTemplate()
        {
            try
            {
                Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
                if ((new int[] { 4, 9, 11 }).Contains(PH.Web.Global.WebContext.CurrentSchool.SchType))
                    workbook.Open(Server.MapPath("/RootFiles/Templates/template_class_2.xls"));
                else
                    workbook.Open(Server.MapPath("/RootFiles/Templates/template_class_1.xls"));
                var buffer = workbook.SaveToStream().ToArray();
                return File(buffer, "application/octet-stream", "班级导入模板.xls");
            }
            catch (Exception ex)
            {
                Common.Logger.Error("", ex);
                return Content("服务器运行出错，请联系开发商。");
            }
        }
    }
}
